/*
 * @Author: kanglang
 * @Date: 2021-05-12 14:19:00
 * @LastEditors: kanglang
 * @LastEditTime: 2021-05-12 14:19:36
 * @Description: 
 */
package com.yuanduo_app.base;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;

import com.xujl.fastlib.base.BaseActivity;
import com.xujl.fastlib.base.BaseModel;
import com.xujl.fastlib.base.BaseView;
import com.xujl.fastlib.consts.PermissionsHelper;

import com.yuanduo_app.event.PermissionsEvent;
import com.yuanduo_app.window.CommonInfoWindow;

import org.greenrobot.eventbus.EventBus;

import androidx.annotation.Nullable;


/**
 * rn封装基类act
 * `
 *
 * @param <M>
 * @param <V>
 */
public abstract class BaseRNActivity<M extends BaseModel, V extends BaseView> extends BaseActivity<M, V> {

    //设置字体为默认大小，不随系统字体大小改而改变
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        if (newConfig.fontScale != 1)//非默认值
            getResources();
        super.onConfigurationChanged(newConfig);
    }


    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        if (res.getConfiguration().fontScale != 1) {//非默认值
            Configuration newConfig = new Configuration();
            newConfig.setToDefaults();//设置默认
            res.updateConfiguration(newConfig, res.getDisplayMetrics());
        }
        return res;
    }

    @Override
    protected void onCreate (@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void permissionsRefuse (String[] permissions) {
        final CommonInfoWindow infoWindow = new CommonInfoWindow(this,
                CommonInfoWindow.createBuilder()
                        .setTitle("系统提示")
                        .setInfo("缺少" + PermissionsHelper.getNoPermissionsStrings(exposeContext(), permissions) + "，是否去手动开启权限？")
                        .setRightStr("去设置")
                        .setLeftStr("退出")
        );
        infoWindow.setCallback(code -> {
            if (code == CommonInfoWindow.RIGHT_BTN) {
                Intent localIntent = new Intent();
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                if (Build.VERSION.SDK_INT >= 9) {
                    localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                    localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                } else if (Build.VERSION.SDK_INT <= 8) {
                    localIntent.setAction(Intent.ACTION_VIEW);
                    localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
                    localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName());
                }
                startActivity(localIntent);
            } else {
                EventBus.getDefault().post(new PermissionsEvent(permissions, false));
                if (permissionsRefuseExit()) {
                    finish();
                }
            }
        });
        infoWindow.showPopupWindow();
    }

    /**
     * 拒绝去开启权限后是否退出当前页面
     *
     * @return
     */
    public boolean permissionsRefuseExit () {
        return false;
    }
}
